package com.zxx.pattern.prototypePattern.prototype3_X;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zxx on 2020/12/23
 */
public class WordDocument implements Cloneable {

    // 文本
    private String mText;
    // 图片名列表
    private ArrayList<String> mImages = new ArrayList<String>();

    public WordDocument() {
        System.out.println("----------- WordDocument 构造函数 -----------");
    }

    @Override
    protected WordDocument clone() {
        try {
            WordDocument doc = (WordDocument) super.clone();
            doc.mText = this.mText;   //非引用对象浅拷贝不影响原始数据
            //doc.mImages = this.mImages; //引用对象浅拷贝(指向同一地址)会影响原始数据


            doc.mImages =(ArrayList<String>)this.mImages.clone();//引用类型深拷贝
            return doc;
        } catch (Exception e) {
        }
        return null;
    }

    public String getText() {
        return mText;
    }

    public void setText(String mText) {
        this.mText = mText;
    }

    public List<String> getImages() {
        return mImages;
    }

    public void addImage(String mImages) {
        this.mImages.add(mImages);
    }

    /**
     * 打印文档内容
     */
    public void showDocument() {
        System.out.println("----------- WordDocument start -----------");
        System.out.println("Text : " + mText);
        System.out.println("Images List: ");
        for (String imgName : mImages) {
            System.out.println("image name : " + imgName);
        }
        System.out.println("----------- WordDocument End -----------");
    }

}
